﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraBars.Ribbon;
using Sunboxsoft.WinForm;
using DevExpress.XtraBars;
using DevExpress.Utils;

namespace Sunboxsoft.WinForm.Shell.Utils
{
    class RibbonUtil
    {
        private static BarButtonItem s_currentCheckButtonItem = null;

        public static void ApplyToRibbon(RibbonControl ribbonControl, List<ActionNode> rootNodes)
        {
            ribbonControl.Pages.Clear();

            ImageCollection iamges = new ImageCollection(true);
            iamges.ImageSize = new System.Drawing.Size(24, 24);
            ImageCollection largeImages = new ImageCollection(true);
            largeImages.ImageSize = new System.Drawing.Size(64, 64);

            for (int index = 0; index < rootNodes.Count; index++)
            {
                ActionNode node = rootNodes[index];
                RibbonPage page = new RibbonPage();
                node.UIObjects.Add(page);
                page.Name = node.Name;
                page.Text = node.Caption;
               
                if (node.Image != null)
                {
                    iamges.AddImage(node.Image);
                    page.ImageIndex = iamges.Images.Count - 1;
                }
                ribbonControl.Pages.Add(page);
                if (node.Children != null)
                {
                    ApplayToRibbon(ribbonControl,iamges, largeImages, page, node.Children);
                }
            }

            ribbonControl.Images = iamges;
            ribbonControl.LargeImages = largeImages;
        }

        private static void ApplayToRibbon(RibbonControl ribbonControl, ImageCollection images, ImageCollection largeImages, RibbonPage page, List<ActionNode> actions)
        {
            for (int index = 0; index < actions.Count; index++)
            {
                ActionNode node = actions[index];
                RibbonPageGroup group = new RibbonPageGroup();
                node.UIObjects.Add(group);

                if (node.Image != null)
                {
                    images.AddImage(node.Image);
                    group.ImageIndex = images.Images.Count - 1;
                }

                group.Name = actions[index].Name;
                group.Text = actions[index].Caption;
                page.Groups.Add(group);
                if (node.Children != null)
                {
                    ApplayToRibbon(ribbonControl, images, largeImages, page, group, node.Children);
                }
            }
        }

        private static void ApplayToRibbon(RibbonControl ribbonControl, ImageCollection images, ImageCollection largeImages, RibbonPage page, RibbonPageGroup group, List<ActionNode> actions)
        {
            for (int index = 0; index < actions.Count; index++)
            {
                ActionNode node = actions[index];
                BarButtonItem button = null;
                

                if (actions.Count < 3)
                {
                    button = new BarLargeButtonItem();
                }
                else
                {
                    button = new BarButtonItem();
                }
                node.UIObjects.Add(button);

                if (node is CheckActionNode)
                {
                    button.ButtonStyle = BarButtonStyle.Check;
                    button.GroupIndex = (node as CheckActionNode).CheckGroupIndex;
                }


                button.Name = actions[index].Name;
                button.Caption = actions[index].Caption;
                button.Tag = node;
                if (actions[index].ClickCallback != null)
                {
                    button.ItemClick += new ItemClickEventHandler(button_ItemClick);
                }

                if (node.Image != null)
                {
                    images.AddImage(node.Image);
                    button.ImageIndex = images.Images.Count - 1;
                }
                if (node.LargeImage != null)
                {
                    largeImages.AddImage(node.LargeImage);
                    button.LargeImageIndex = largeImages.Images.Count - 1;
                }
                ribbonControl.Items.Add(button);
                group.ItemLinks.Add(button);
            }
        }

        static void button_ItemClick(object sender, ItemClickEventArgs e)
        {
            BarButtonItem button = e.Item as BarButtonItem;
            if (button != null)
            {
                if (button.ButtonStyle == BarButtonStyle.Check)
                {
                    if (s_currentCheckButtonItem == button)
                    {
                        return;
                    }
                    s_currentCheckButtonItem = button;
                }
                ActionNode node = button.Tag as ActionNode;
                // 处罚布局调整
                // 显示左侧菜单
                UIContext.Current.NavigatorManager.ShowSecondaryActions(node);

                if (node.ClickCallback != null)
                {
                    node.ClickCallback(sender, e);
                }
            }
        }
    }
}
